package yang.simpleMain.ByteUtil;

/**
 * 将输入的字节转换可读模式
 * @author xinxin
 *
 */
public class ByteUtil {
	/** bit */
	public static int UNIT_BIT = 0;
	/** byte */
	public static int UNIT_BYTE = 1;
	/** KB */
	public static int UNIT_KB = 2;
	/** MB */
	public static int UNIT_MB = 3;
	/** GB */
	public static int UNIT_GB = 4;
	/** TB */
	public static int UNIT_TB = 5;
	/** PB */
	public static int UNIT_PB = 6;
	
	/** 单位名称 */
	public static String[] UNIT_NAME = {"b", "B", "KB", "MB", "GB", "TB", "PB"};
	/** 单位比例 */
	public static int[] UNIT_RATIO = {8, 1024, 1024, 1024, 1024, 1024, 1024};
	
	public static String format(long nums, int type) {
		int ret = 0;
		String pre = "";
		ret = (int) (nums % UNIT_RATIO[type]);
		nums = nums / UNIT_RATIO[type];
		if (nums > 0) {
			pre = format(nums, type + 1);
		}
		return pre + "\t" + ret + UNIT_NAME[type];
	}
	
	public static void main(String[] args) {
		System.out.println(format(96757023, UNIT_BIT));
	}
	

}
